﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TravelAgentsDesktopDBApp
{
    public partial class FormSuppliers : Form
    {

        //class var
        Suppliers _selectedSup = null;

        public FormSuppliers()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void showList()
        {
            //function to show the list pane and hide the inputs

            gpbSuppliersInfo.Visible = false;
            gpbSuppliersInfo.Enabled = false;
            btnSupBack.Visible = false;
            btnSupBack.Enabled = false;
            gpbSuppliersList.Visible = true;
            gpbSuppliersList.Enabled = true;
        }

        private void showInput()
        {
            //function to show the inputs and hide the list pane

            gpbSuppliersInfo.Visible = true;
            gpbSuppliersInfo.Enabled = true;
            btnSupBack.Visible = true;
            btnSupBack.Enabled = true;
            gpbSuppliersList.Visible = false;
            gpbSuppliersList.Enabled = false;
        }

        private void populateLstSup()
        {
            //function to populate lstSuppliers with suppliers which match the search terms

            lstSuppliers.Items.Clear();
            lstSuppliers.Items.Add("CREATE NEW SUPPLIER");
            lstSuppliers.Items.Add("");

            List<Suppliers> suppliers = new Search().filterSupName(this);

            foreach (Suppliers supplier in suppliers)
            { lstSuppliers.Items.Add(supplier); }

        }

        private void populateLstSup(Suppliers entry)
        {
            //function to populate lstSuppliers with selected supplier details
            //and some commands

            lstSuppliers.Items.Clear();

            lstSuppliers.Items.Add("ID: " + entry.SupplierId);
            lstSuppliers.Items.Add("NAME: " + entry.SupName);
            lstSuppliers.Items.Add("");
            lstSuppliers.Items.Add("EDIT");
            lstSuppliers.Items.Add("DELETE");
            lstSuppliers.Items.Add("BACK");

        }

        private void populateSupDeets(Suppliers entry)
        {
            txtSupplierId.Enabled = false;
            txtSupplierId.Text = entry.SupplierId.ToString();
            txtSupplierName.Text = entry.SupName;
        }

        private void resetInputs()
        {
            //clear Sup name
            txtSupplierName.Text = "";

            if (_selectedSup != null)
            {
                txtSupplierId.Enabled = false;
                txtSupplierId.Text = _selectedSup.SupplierId.ToString();
            }
            else
            {
                txtSupplierId.Enabled = true;
            }

            //clear the search field as well
            txtSearchSuppliers.Text = "";

            //clear selected
            _selectedSup = null;

        }

        private void FormSuppliers_Load(object sender, EventArgs e)
        {
            populateLstSup();
            showList();
            txtSupplierId.Enabled = false;
        }

        private void txtSearchSuppliers_TextChanged(object sender, EventArgs e)
        {
            if (_selectedSup == null) { populateLstSup(); }
        }

        private void btnSupRes_Click(object sender, EventArgs e)
        {
            if (_selectedSup != null) { populateSupDeets(_selectedSup); }
            else { resetInputs(); }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            if (_selectedSup == null)
            {
                resetInputs();
                populateLstSup();
            }

            else { populateLstSup(_selectedSup); }

            showList();
        }

        private void lstSuppliers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstSuppliers.SelectedItem != null)
            {
                if (lstSuppliers.SelectedItem.GetType() == typeof(Suppliers))
                {
                    _selectedSup = (Suppliers)lstSuppliers.SelectedItem;
                    populateLstSup(_selectedSup);
                }
                else
                {
                    switch (lstSuppliers.SelectedItem.ToString())
                    {
                        case "CREATE NEW SUPPLIER":
                            btnSupMulti.Text = "SAVE";
                            btnSupBack.Text = "BACK TO LISTING";
                            resetInputs();
                            showInput();
                            break;

                        case "EDIT":
                            btnSupMulti.Text = "UPDATE";
                            btnSupBack.Text = "BACK TO SELECTED SUPPLIER";
                            populateSupDeets(_selectedSup);
                            showInput();
                            break;

                        case "BACK":
                            resetInputs();
                            populateLstSup();
                            showList();
                            break;

                        case "DELETE":
                            //ask user for delete confirmation, otherwise do nothing

                            if (_selectedSup != null)
                            {

                                //show a msg box to confirm deletion
                                DialogResult confirm = MessageBox.Show("Do you really want to delete " + _selectedSup.SupName + "?",
                                    "Confirm Supplier Deletion", MessageBoxButtons.YesNo);

                                if (confirm == DialogResult.Yes)
                                {
                                    new DbAccess().DeleteSuppliers(_selectedSup);
                                    resetInputs();
                                    populateLstSup();
                                    showList();
                                }
                            }

                            else
                            { MessageBox.Show("Could not delete!\nUnknown error!"); }

                            break;

                        default: break;
                    }
                }
            }
        }

        private Suppliers getSupplier(string cmd)
        {
            //function validates user input as it tries to make a new package
            bool isValid = true;
            Suppliers entry = new Suppliers();


            foreach (Control ctrl in gpbSuppliersInfo.Controls)
            {
                switch (ctrl.Name)
                {
                    case "txtSupplierName":
                        if (txtSupplierName.Text != "")
                            { entry.SupName = txtSupplierName.Text; }
                        else
                        {
                            isValid = false;
                            MessageBox.Show("Supplier name is required");
                        }

                        break;

                    case "txtSupplierId":
                        int id;
                        if (int.TryParse(txtSupplierId.Text, out id))
                        {
                            List<Suppliers> suppliers = new DbAccess().SelectSuppliers();
                            foreach (Suppliers supEntry in suppliers)
                            {
                                if ((id == supEntry.SupplierId) && (cmd.Equals("INSERT")))
                                {
                                    isValid = false;
                                    MessageBox.Show("Unique supplier id is required");
                                    break;
                                }
                            }
                            entry.SupplierId = id; 
                        }
                        else
                        {
                            isValid = false;
                            MessageBox.Show("Supplier id is required");
                        }
                        break;

                    default: break;

                }
            }

            if (!isValid) { return null; }
            else { return entry; }

        }

        private void btnSupMulti_Click(object sender, EventArgs e)
        {
            switch (btnSupMulti.Text)
            {
                case "SAVE":

                    Suppliers newCandi = getSupplier("INSERT");
                    if (newCandi != null)
                    {
                        new DbAccess().InsertSuppliers(newCandi);
                        resetInputs();
                        populateLstSup();
                        showList();
                    }

                    break;

                case "UPDATE":

                    Suppliers updCandi = getSupplier("UPDATE");
                    if (updCandi != null) 
                    { 
                        new DbAccess().UpdateSuppliers(updCandi); 
                        resetInputs();
                        populateLstSup();
                        showList();                    
                    }

                    break;

                default: break;


            }
        }

    }
}
